package 链表及其反转_堆栈诠释;

/**
 * @author 冷加宝
 * @version 1.0
 */
public class ListReverse {
    public static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
        }

        public ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode reverseList(ListNode head){
        ListNode prev = null;
        ListNode next = null;
        while(head != null){
            next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }

    public static class DoubleListNode{
        int val;
        DoubleListNode next;
        DoubleListNode last;
        public DoubleListNode(int val){
            this.val = val;
        }
    }

    public static DoubleListNode reverseDoubleList(DoubleListNode head){
        DoubleListNode prev = null;
        DoubleListNode next = null;
        while(head != null){
            next = head.next;
            head.next = prev;
            head.last = next;
            prev = head;
            head = next;
        }
        return prev;
    }
}
